<template>
  <div class="content">
    <el-table
      ref="multipleTableRef"
      :data="tableList"
      style="width: 100%"
      row-key="id"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="35"
        align="center"
        class-name="no-overflow"
        v-if="showSelectColumn"
      />
      <el-table-column v-bind="item" v-for="item in tableColumnConfig" :key="item.property">
        <template #default="{ row }" v-if="item.slotName">
          <slot :name="item.slotName" :row="row"></slot>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right">
        <template #default="{ row }">
          <div class="line-hanle-btn">
            <el-button type="text" :icon="Edit" size="small" @click="handleEditClcik(row)">
              编辑
            </el-button>
            <el-button type="text" :icon="Delete" size="small" @click="handleDeleteClcik(row)">
              删除
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <slot name="footer">
      <div class="pagination" v-if="showFooter">
        <el-pagination
          v-model:currentPage="agentPage"
          v-model:page-size="agentPsize"
          :page-sizes="[10, 20, 30, 40]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="count"
        />
      </div>
    </slot>
  </div>
</template>

<script setup lang="ts">
import { Edit, Delete } from "@element-plus/icons"
import { defineProps, defineEmits, PropType, computed } from "vue"
import { ITableConfigItem } from "../types"
const props = defineProps({
  currentPage: {
    type: Number,
    default: 1
  },
  pageSize: {
    type: Number,
    default: 10
  },
  count: {
    type: Number,
    default: 0
  },
  showFooter: {
    type: Boolean,
    default: true
  },
  tableList: {
    type: Array,
    require: true
  },
  tableColumnConfig: {
    type: Array as PropType<ITableConfigItem[]>,
    require: true
  },
  showSelectColumn: {
    type: Boolean,
    default: () => {
      return true
    }
  }
})

const emits = defineEmits(["pszieChange", "pageChange", "handleEditClcik", "handleDeleteClcik"])

const agentPage = computed({
  get: () => props.currentPage,
  set: newval => emits("pageChange", newval)
})
const agentPsize = computed({
  get: () => props.pageSize,
  set: newval => emits("pszieChange", newval)
})
const handleSelectionChange = (val: any[]) => {
  console.log(val)
}

const handleDeleteClcik = row => {
  emits("handleDeleteClcik", row)
}
const handleEditClcik = row => {
  emits("handleEditClcik", row)
}
</script>

<style scoped lang="scss">
.content {
  margin-top: 20px;
  background: #fff;
  .pagination {
    display: flex;
    flex-direction: row-reverse;
    padding: 10px 10px 10px 0;
  }
}
</style>
